<?php
class Controller_Product_Provider extends Controller_Abstract
{
	function actionList()
	{
		$this->_view['title'] = '列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['product_categories'] = json_encode($product_categories);
		$url_arr = Helper_BSS_Normal::buildCondition();
		$this->_view['url_arr'] = json_encode($url_arr);
		$url_arr['limit'] = $user['ack'];
		$result = Product_Provider::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$provider = $result['data'];
			foreach ($provider as $k => $p)
			{
				$provider[$k]['categories'] = Product_Categories::find('provider_categories_mapping.product_provider_id=?', $p['id'])
					->joinLeft('provider_categories_mapping', '', 'provider_categories_mapping.product_categories_id=product_categories.id')
					->asArray()->getAll(); 
			}
			$page = new Helper_BSS_Page($result['pagination'], url('Product::Provider/List', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['provider'] = $provider;
		}
	}
	
	function actionCreate()
	{
		$this->_view['title'] = '新增';
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['categories'] = $product_categories;
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '新增（保存）';
		if ($this->_context->isPost())
		{
			$provider = Helper_BSS_Normal::mysqlEscape($_POST);
			if ($provider['type'] != Q::ini('custom_flag/product_provider_type/online/value'))
			{
				$provider['wang_wang'] = '';
				$provider['link'] = '';
			}
			$result = Product_Provider::createOrEidt($provider);
			if ($result['ack'] == FAILURE)
			{
				return $this->_redirectMessage('保存失败！', $result['message'],  $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存成功！', '成功保存供应商信息', url('Product::Provider/List'), $result['ack']);
			}
		}
		return $this->_redirect(url('Product::Provider/List'));
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '编辑';
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['categories'] = $product_categories;
		if (empty($_GET['id']))
		{
			return $this->_redirect(url('Product::Provider/List'));
		}
		else
		{
			$provider_detail = Product_Provider::find('id=?', $_GET['id'])->asArray()->getOne();
			$provider_detail['mapping'] = QDB::getConn()->execute('select product_categories_id from provider_categories_mapping where product_provider_id=' . $provider_detail['id'])->fetchCol();
			$user = Purchase_Order::getUser();
			$this->_view['user'] = $user['data'];
			$this->_view['provider_detail'] = $provider_detail;
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '编辑（保存）';
		if ($this->_context->isPOST())
		{
			$provider = Helper_BSS_Normal::mysqlEscape($_POST);
			if ($provider['type'] != Q::ini('custom_flag/product_provider_type/online/value'))
			{
				$provider['wang_wang'] = '';
				$provider['link'] = '';
			}
			$result = Product_Provider::createOrEidt($provider);
			if ($result['ack'] == FAILURE)
			{
				return $this->_redirectMessage('修改失败！', $result['message'],  $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('修改成功！', '成功保存供应商信息。', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '详情';
		$provider = Product_Provider::find('product_provider.id = ?', $_GET['id'])
			->joinLeft('user', 'name as follow_username', 'user.id=product_provider.follow_user_id')
			->asArray()->getOne();
		$provider['mapping'] = QDB::getConn()->execute('select product_categories_id from provider_categories_mapping where product_provider_id=' . $provider['id'])->fetchCol();
		$this->_view['categories'] = Product_Categories::getCategoriesTree();
		$this->_view['provider'] = $provider;
	}
	
	function actionMultipleAddFollowUser()
	{
		$this->_view['title'] = '批量添加跟进人员';
		$follow_user = User::find('id=? and status=?', $_POST['follow_user_id'], Q::ini('custom_flag/user_status/on_duty/value'))->asArray()->getOne();
		if ($follow_user)
		{
			$provider = Product_Provider::find('id in (?)', explode(',', $_POST['provider_ids']))->getAll();
			foreach ($provider as $p)
			{
				$p->follow_user_id = $follow_user['id'];
				$p->save();
			}
		}
		return $this->_redirectMessage('批量添加成功！', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
	}
	
	function actionExportChecked()
	{
		$this->_view['title'] = '按勾选导出';
		if ($this->_context->isPOST())
		{
			$ids = json_decode($_POST['ids']);
			$search_result = Product_Provider::search(array('ids' => $ids), 0, 0);
			foreach ($search_result['data'] as $k => $p)
			{
				$search_result['data'][$k]['categories'] = Product_Categories::find('provider_categories_mapping.product_provider_id=?', $p['id'])
					->joinLeft('provider_categories_mapping', '', 'provider_categories_mapping.product_categories_id=product_categories.id')
					->asArray()->getAll(); 
			}
			if (count($search_result['data']) <= 1000)
			{
				Helper_BSS_XLS::ProductProviderExport($search_result['data']);
			}
			else
			{
				return $this->_redirectMessage('导出失败', '导出记录超过一千条..', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionExportSearch()
	{
		$this->_view['title'] = '按搜索条件导出';
		if ($this->_context->isPOST())
		{
			$url_arr = json_decode($_POST['url_arr'], 1);
			$search_result = Product_Provider::search($url_arr, 0, 0, 'product_provider.id asc');
			foreach ($search_result['data'] as $k => $p)
			{
				$search_result['data'][$k]['categories'] = Product_Categories::find('provider_categories_mapping.product_provider_id=?', $p['id'])
					->joinLeft('provider_categories_mapping', '', 'provider_categories_mapping.product_categories_id=product_categories.id')
					->asArray()->getAll(); 
			}
			if (count($search_result['data']) <= 1000)
			{
				Helper_BSS_XLS::ProductProviderExport($search_result['data']);
			}
			else
			{
				return $this->_redirectMessage('导出失败', '导出记录超过一千条..', $_SERVER['HTTP_REFERER'], FAILUER);
			}
		}
	}
	
}